package com.lhf.userservice.service.impl;

import com.lhf.userservice.dao.FvsSysRoleDao;
import com.lhf.userservice.dao.FvsSysUserDao;
import com.lhf.userservice.entity.FvsSysUser;
import com.lhf.userserviceapi.dto.FvsSysRoleDTO;
import com.lhf.userserviceapi.dto.FvsSysUserDTO;
import com.lhf.userserviceapi.service.UserService;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p></p>
 *
 * @author liuhf
 * @createTime 2021/4/20 15:22
 * @since 1.0
 */
@Component
@DubboService
public class UserServiceImpl implements UserService {

    @Resource
    private FvsSysUserDao fvsSysUserDao;

    @Resource
    private FvsSysRoleDao fvsSysRoleDao;

    /**
     * 根据用户名查询用户
     *
     * @param username
     * @return
     */
    @Override
    public FvsSysUserDTO findUserByUsername(String username) {
        FvsSysUser user = this.fvsSysUserDao.findUserByUsername(username);
        FvsSysUserDTO dto = null;
        if (user != null) {
            dto = new FvsSysUserDTO();
            BeanUtils.copyProperties(user, dto);
        }
        System.out.println(dto);
        return dto;
    }

    @Override
    public List<FvsSysRoleDTO> findRoleByPath(String path) {
        List<FvsSysRoleDTO> role = this.fvsSysRoleDao.findRoleByPath(path);
        if (StringUtils.hasText(path.trim()) && CollectionUtils.isEmpty(role)) {
            int index = path.lastIndexOf("/");
            role = this.findRoleByPath(path.substring(0, index));
        }
        return role;
    }
}
